For(TLocal) Method (Int64, Int64, Func(TLocal), Func(Int64, ParallelLoopState, TLocal, TLocal), Action(TLocal))

Task Parallel System.Threading

Executes a for loop in which iterations may run in parallel. Supports 64-bit indices.

Namespace:  System.Threading.Tasks
Assembly:  System.Threading (in System.Threading.dll)

Syntax

Visual Basic (Declaration)
Public Shared Function For(Of TLocal) ( _
	fromInclusive As Long, _
	toExclusive As Long, _
	localInit As Func(Of TLocal), _
	body As Func(Of Long, ParallelLoopState, TLocal, TLocal), _
	localFinally As Action(Of TLocal) _
) As ParallelLoopResult
C#
public static ParallelLoopResult For<TLocal>(
	long fromInclusive,
	long toExclusive,
	Func<TLocal> localInit,
	Func<long, ParallelLoopState, TLocal, TLocal> body,
	Action<TLocal> localFinally
)

Parameters

fromInclusive
Type: System..::.Int64
The start index, inclusive.
toExclusive
Type: System..::.Int64
The end index, exclusive.
localInit
Type: System..::.Func<(Of <(TLocal>)>)
The function delegate that returns the initial state of the local data for each thread.
body
Type: System..::.Func<(Of <(Int64, ParallelLoopState, TLocal, TLocal>)>)
The delegate that is invoked once per iteration.
localFinally
Type: System..::.Action<(Of <(TLocal>)>)
The delegate that performs a final action on the local state of each thread.

Type Parameters

TLocal
The type of the thread-local data.

Return Value

A ParallelLoopResult structure that contains information on what portion of the loop completed.

Remarks

The body delegate is invoked once for each value in the iteration range: [fromInclusive, toExclusive). It is provided with the following parameters: the iteration count (an Int64), a ParallelLoopState instance that may be used to break out of the loop prematurely, and some local state that may be shared amongst iterations that execute on the same thread.

The localInit delegate is invoked once for each thread that participates in the loop's execution and returns the initial local state for each of those threads. These initial states are passed to the first body invocations on each thread. Then, every subsequent body invocation returns a possibly modified state value that is passed to the next body invocation. Finally, the last body invocation on each thread returns a state value that is passed to the localFinally delegate. The localFinally delegate is invoked once per thread to perform a final action on each thread's local state.

Exceptions

ExceptionCondition
System..::.ArgumentNullExceptionThe exception that is thrown when the body argument is null.
System..::.ArgumentNullExceptionThe exception that is thrown when the localInit argument is null.
System..::.ArgumentNullExceptionThe exception that is thrown when the localFinally argument is null.
System..::.AggregateExceptionThe exception that is thrown to contain an exception thrown from one of the specified delegates.

See Also